package org.crazyit.activiti_d3._4_CandidateQuery;

import org.activiti.engine.IdentityService;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.identity.Group;
import org.activiti.engine.identity.User;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.task.IdentityLink;

import java.util.List;
import java.util.UUID;

import static org.crazyit.activiti_d3._1_UserCandidate.UserCandidate.createUser;
import static org.crazyit.activiti_d3._2_UserGroupCandidate.UserGroupCandidate.createUserGroup;

public class CandidateQuery {

    public static void main(String[] args) {

        //1. 创建流程引擎
        ProcessEngine engine = ProcessEngines.getDefaultProcessEngine();

        //2 得到流程存储服务实例
        RepositoryService repositoryService = engine.getRepositoryService();

        //3. 得到身份服务组件
        IdentityService identityService = engine.getIdentityService();

        //4. 部署流程描述文件
        Deployment dep  = repositoryService.createDeployment().addClasspathResource("candidateQuery.bpmn").deploy();

        //5. 添加两个用户
        String id1 = UUID.randomUUID().toString();
        String id2 = UUID.randomUUID().toString();
        createUser(identityService,id1,"angus","young","abc@163.com","123");
        createUser(identityService,id2,"angus2","young2","abc@163.com","123");

        //6. 添加两个用户组
        String id3 = UUID.randomUUID().toString();
        String id4 = UUID.randomUUID().toString();
        createUserGroup(identityService, id3,"group1","用户组1");
        createUserGroup(identityService, id4,"group2","用户组2");

        //7. 查询流程定义实体
        ProcessDefinition def = repositoryService.createProcessDefinitionQuery().deploymentId(dep.getId()).singleResult();

        //8. 设置权限数据
        repositoryService.addCandidateStarterUser(def.getId(),id1);
        repositoryService.addCandidateStarterUser(def.getId(),id2);
        repositoryService.addCandidateStarterGroup(def.getId(),id3);
        repositoryService.addCandidateStarterGroup(def.getId(),id4);

        //9. 根据用户查询有权限的流程定义
        List<ProcessDefinition> defs = repositoryService.createProcessDefinitionQuery().startableByUser(id1).list();
        //结果为1
        System.out.println("用户ID为" + id1 + "有权限的流程定义为:");
        for (ProcessDefinition definition : defs) {
            System.out.println("   " + definition.getName());
        }

        //10. 根据流程定义查询用户组数据
        List<Group> groups = identityService.createGroupQuery().potentialStarter(def.getId()).list();
        System.out.println("以下用户组对id 为" + def.getId() + "流程定义有权限:");
        for (Group group : groups) {
            System.out.println("   " + group.getName());
        }

        //11. 根据流程定义查询用户数据
        List<User> users = identityService.createUserQuery().potentialStarter(def.getId()).list();
        System.out.println("以下用户对id 为" + def.getId() + "流程定义有权限:");

        //12. 根据流程定义查询全部的IdentityLink (ACT_RU_IDENTITYLINK 表) 数据
        List<IdentityLink> links = repositoryService.getIdentityLinksForProcessDefinition(def.getId());
        //结果为4
        System.out.println("与流程定义相关的数据量:" + links.size());

    }

}
